#!/usr/bin/env python
# coding=utf-8

if __name__ == '__main__':
    """ for file debug"""
    import sys,os
    sys.path.insert(0,os.path.join( os.getcwd(), '..' ))

import threading
from threading import Thread,Event,Lock
from vavava.base import LogAdapter

__author__ = 'vavava'

class BaseThread(Thread):
    def __init__(self, name="<unknown_thread>",log=None):
        Thread.__init__(self,target=self.run)
        self.log = LogAdapter(log)
        self.setName(name)
        self.setDaemon(True)
        self._is_paused=threading.Event()
        self._is_paused.clear()
        self._is_running=threading.Event()
        self._is_running.clear()

    def run(self):
        """
        usage:
            while self.IsRunning:
                if not self.IsPaused:
                    ....
                    do something
                    ....
                else:
                    wait for a while
        """

    def running_start(self):
        if not self.IsRunning:
            self._is_running.set()
            self.start()
        else:
            raise "thread is running,can not start twice"

    def running_stop(self):
        if self.IsRunning:
            self._is_running.clear()
        else:
            raise "thread is not running"

    def running_pause(self):
        if self.IsPaused:
            raise "thread already paused"
        self._is_paused.set()

    def running_resume(self):
        if self.IsPaused:
            self._is_paused.clear()
        else:
            raise "thread already resumed"

    @property
    def IsAlive(self):
        # thread is alive, maybe paused, maybe running
        # thread is not alive: surely not running
        return self.is_alive()

    @property
    def IsRunning(self):
        # thread is running: maybe paused, surely alive
        # thread is not running: out of work routine
        return self._is_running.is_set()

    @property
    def IsPaused(self):
        return self._is_paused.is_set()

class TThread(BaseThread):
    def __init__(self,seq):
        BaseThread.__init__(self)
        self.seq=seq

    def run(self):
        while self.IsRunning:
            if self.IsPaused:
                import time
                print("_thread waiting %d "%self.seq)
                time.sleep(1)
            else:
                print("_thread running %d "%self.seq)
        print("_thread out %d"%self.seq)

def main():
    import time
    tt1=TThread(1)
    #tt2=TThread(2)
    #tt3=TThread(3)

    print('main _thread(%f):start'%time.clock())
    tt1.running_start()
    #tt2.start()
    #tt3.start()
    try:
        while True:
            if not tt1.IsPaused:
                tt1.running_pause()
            else:
                tt1.running_resume()
            time.sleep(2)
    except(KeyboardInterrupt):
        print('main _thread(%f):stop timer'%time.clock())
        tt1.running_stop()
        #tt2.stop()
        #tt3.stop()
        print('main _thread(%f):stopped'%time.clock())

if __name__ == '__main__':
    main()

